<!--renshuai-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="cateName">
        <el-input
            v-model="queryParams.cateName"
            placeholder="名称"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="status">
        <el-select v-model="queryParams.status" placeholder="状态" clearable>
          <el-option  v-for="(item,index) in statusList" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['cms:cate:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编码" align="center" prop="cateCode"  :show-overflow-tooltip="true"/>
      <el-table-column label="名称" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/cms/prod/cate_item/index/' + scope.row.cateCode" class="link-type">
            <span>{{ scope.row.cateName }}</span>
          </router-link>
        </template>

      </el-table-column>

      <el-table-column label="排序" align="center" prop="sortNum" :show-overflow-tooltip="true"/>
      <el-table-column label="状态" align="center" prop="status" width="90">
        <template slot-scope="scope">
          <dict-tag :options="statusList" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              type="text"
              size="small"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['cms:cate:edit']">修改</el-button>
          <el-button
              type="text"
              size="small"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['cms:cate:del']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改话框 -->
    <el-dialog :title="title" :visible.sync="open" width="780px" append-to-body center :before-close="handleClose">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="编码" prop="cateCode">
              <el-input v-model="form.cateCode" :disabled="!isAdd"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="名称" prop="cateName">
              <el-input v-model="form.cateName"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="状态" prop="status">
              <el-switch
                  v-model="form.status"
                  active-value="1"
                  inactive-value="0"
                  active-color="#13ce66"
                  inactive-color="#ff4949">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序" prop="sortNum">
              <el-input v-model="form.sortNum"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="onSubmitForm">保 存</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {delCateByCode, getCateByCode, insertCate, listCate, updateCate} from "@/api/cms/prod/cate";

export default {
  name: "CateList",
  data() {
    return {
      statusList:[
        {
          raw:{
            listClass:"default",
            cssClass:""
          },
          value:"",
          label:"全部"
        },
        {
          raw:{
            listClass:"primary",
            cssClass:""
          },
          value:"1",
          label:"正常"
        },
        {
          raw:{
            listClass:"info",
            cssClass:""
          },
          value:"0",
          label:"关闭"
        }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      cateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isAdd:false,//是否新增
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: undefined,
        cateNameComp:"like",
        cateName: undefined,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        cateName:  [
          {
            validator:(rule, value, callback)=>{
              if(!value){
                callback(new Error("名称不能为空"))
                this.$notify.error({
                  title: '错误',
                  message: '名称不能为空'
                });
              }else{
                callback(); //成功后不传入错误参数即可
              }
            }, trigger: "blur" }
        ],
        cateCode: [
          {
            validator:(rule, value, callback)=>{
              if(!value){
                callback(new Error("编码不能为空"))
                this.$notify.error({
                  title: '错误',
                  message: '编码不能为空'
                });
              }else{
                callback(); //成功后不传入错误参数即可
              }
            }, trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listCate(this.queryParams).then(response => {
        this.cateList = response.data;
        this.total = response.total != NaN ?Number(response.total):undefined;
        this.loading = false;
      });
    },

    /**
     * 详情按钮
     * @param row
     */
    handleUpdate(row){
      let cateCode=row.cateCode;
      getCateByCode(cateCode).then(response => {
        this.form = response.data;
        this.open = true;
        this.isAdd=false;
        this.title = "详细信息";
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
       this.$modal.confirm('是否确认删除名称为"' + row.cateName + '"的数据项？').then(function() {
        return delCateByCode(row.cateCode);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },

    onSubmitForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.isAdd){
            insertCate(this.form).then(response => {
              this.$modal.msgSuccess("保存成功");
              this.open = false;
              this.getList();
            });
          }else{
            updateCate(this.form).then(response => {
              this.$modal.msgSuccess("保存成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    handleAdd(){
      this.reset();
      this.open = true;
      this.isAdd= true;
      this.title = "新增";
    },

    // 取消按钮
    handleClose() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        cateCode:undefined,
        cateName:undefined,
        status: "1"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },


  }
};
</script>
<style lang="scss" scoped>
</style>